<?php

$thisPage = "forgottenpassword";
$subpage = getRequestParameter("subpage", false);

TemplateHelper::useLayout("form");

Log::info("Loading page functions: $thisPage");

addDataService("forgottenpassword");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "forgottenpassword" : { return service_createPassword($params); }
    }
}

function buildLayout(&$smarty) {
    global $subpage;

    Log::debug("Executing function: buildLayout");
    if ($subpage === false) {
        $smarty->assign("form", getForgottenPasswordFormData($smarty));
    } else if ($subpage == "success") {
        $smarty->assign("form", getSuccessCreatepassword($smarty));
    } else if ($subpage == "failed") {
        $smarty->assign("form", getFailCreatepassword($smarty));
    }
}

function service_createPassword($params) {
    Log::info("executing service: createPassword");
    $exists = false;
    $success = true;
    $r = "";
    try {
        Database::loadModel("user");
        $exists = Database::getModel()->userExists($params['username']);
    }
    catch (Exception $e) {
        $success = false;
        $errorMsg = $e->getMessage();
    }
	if ($exists == true){
		$userdata = Database::getModel()->getUserData($params['username']);
		$r = null;
		if ($userdata['email'] != $params['email']){
			$success = false;
			$errorMsg = "Email is incorrect";			
		}else{
			$r = paramEmail($userdata);
		}
		if ($r != null){
			$success = false;
			$errorMsg = $r;
		}
	}else{
		$success = false;
		$errorMsg = "Username is incorrect";

	}
    if ($success) {
        Log::info("Finished service: login status: success");
        $r = serviceSuccessfulWithRedirect(array(), "forgottenpassword", "success");
    } else {
        Log::info("Finished service: login status: failure");
        $r = serviceFailure(array(), $errorMsg);
    }
    Log::info("Finishing service: createPassword");
    return $r;
}


function getPageHeader() {
    Log::debug("Executing function: getPageHeader");
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    Log::debug("Executing function: getPageFooter");
    return TemplateHelper::getDefaultPageFooter();
}

function getForgottenPasswordFormData(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getRegisterFormData");
    return array(
        title => "Forgotten password to portal",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "forgottenpassword_content"))
    );
}

function getSuccessCreatepassword(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getSuccessCreatepassword");
	$username = getRequestParameter("username");
	$ticket = getRequestParameter("ticket");
	$smarty->assign('username',$username);
	$smarty->assign('ticket',$ticket);	
	$content = $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "createpassword_success"));
   	return array(
        title => "Create password success",
        content => $content,
  	);
}

function getFailCreatepassword(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getFailCreatepassword");
    return array(
        title => "Create password unsuccess",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "createpassword_faild")),
    );
}

function paramEmail($userdata){
	global  $_CONFIG;
	$to = $userdata['email'];
	$emailName = "forgottenpassword";
	$params = array();
	$params['firstname'] = $userdata['firstname'];
	$params['lastname'] = $userdata['lastname'];
	$params['username'] = $userdata['username'];
	$code = hash(md5, rand());
	$params['password'] = substr($code, 0, 8);
	$pw = md5($params['password']);
	$res = Database::executeQuery("update users set password = '" . $pw . "' where username = '" . $params['username'] . "'");
	$r = sendEmail($emailName, $to, $params);
	return $r;
}

function updateUserData($password){
	$userdata = Database::getModel()->updateUserData($params['username']);
}

?>
